/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski   *
 *   g.latarowski@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/



// local
#include "window.h"




// ===========================================================================
//                                 Window
// ===========================================================================


// ------------------------------- Window() -----------------------------------
Window::Window() : QMainWindow()
{
        // ui
    setupUi( this );

    statusBar()->showMessage( tr("Load an image.") );
    
    // ==============================================
    // setup graphicsROIEditor
    
        // create scene 
    _pSceneROIEditor = new QGraphicsScene();
    _pSceneROIEditor->setItemIndexMethod( QGraphicsScene::NoIndex );
    _pGraphicsPixmapItem = NULL;
        // a gradient background
    graphicsROIEditor->setScene( _pSceneROIEditor );
    graphicsROIEditor->setBackgroundBrush( Qt::black );
    
}
// ---------------------------------------------------------------------------


// ----------------------------- ~Window() -----------------------------------
Window::~Window()
{
}
// ---------------------------------------------------------------------------


// ----------------------- on_actionLoadImage_triggered() --------------------
void Window::on_actionLoadImage_triggered()
{
    QString dir = ".";
    QString imageFile = QFileDialog::getOpenFileName
            ( this,
              tr("Select image file"),
              dir,
              tr("Images (*.bmp *.png *.xpm *.jpg *.jpeg)")
            );
    
    if ( !imageFile.isEmpty() )
    {
        // load content
        QPixmap imagePixmap( imageFile );
        if( !imagePixmap.isNull() )
        {
                // remove old pixmap item from ROI scene 
            if( _pGraphicsPixmapItem )
            {
                _pSceneROIEditor->removeItem( _pGraphicsPixmapItem );
                delete _pGraphicsPixmapItem;
            }
                // create new pixmap item on the scene 
            _pGraphicsPixmapItem = _pSceneROIEditor->addPixmap( imagePixmap );
                // set scene rect clipped to pixmaps size 
            _pSceneROIEditor->setSceneRect( _pGraphicsPixmapItem->pixmap().rect() );
                // show status message
            statusBar()->showMessage( tr("Image \"%1\" loaded").arg( imageFile ), 5000 );
                // fit image in window
            if( imagePixmap.width() > imagePixmap.height() ) {
                on_actionFitToWidth_triggered();
            } else {
                on_actionFitToHeight_triggered();
            }
            _pROI = new GROI( _pGraphicsPixmapItem );
            _pSceneROIEditor->addItem( _pROI );
            _pROI->addChildToScene();
                
        }
        else
        {
                // pixmap is invalid
                // show warning
            QMessageBox::warning( this,
                                  tr("Adjuster Warning"),
                                  tr("Cannot open file \"%1\": "
                                          "Unrecognized file type." ).arg( imageFile ),
                                  QMessageBox::Ok
                                );

        }
    }
}
// ---------------------------------------------------------------------------


// ------------------------- on_actionZoomIn_triggered() ---------------------
void Window::on_actionZoomIn_triggered()
{
    graphicsROIEditor->scale( 1.2, 1.2 );
}
// ---------------------------------------------------------------------------


// ------------------------- on_actionZoomOut_triggered() ---------------------
void Window::on_actionZoomOut_triggered()
{
    graphicsROIEditor->scale( 1 / 1.2, 1 / 1.2 );
}
// ---------------------------------------------------------------------------


// ----------------------- on_actionOriginalSize_triggered() -----------------
void Window::on_actionOriginalSize_triggered()
{
    graphicsROIEditor->setMatrix( QMatrix(), false );
}
// ---------------------------------------------------------------------------


// ------------------ on_actionFitToWidth_triggered() ------------------------
void Window::on_actionFitToWidth_triggered()
{
    if( !_pGraphicsPixmapItem ) return;

    double pixmapWidth = (double) _pGraphicsPixmapItem->pixmap().width();
    double viewWidth   = (double) graphicsROIEditor->size().width();

    double scale = viewWidth / pixmapWidth;
    
    graphicsROIEditor->setMatrix( QMatrix().scale( scale, scale ), false );
}
// ---------------------------------------------------------------------------

    
// ----------------- on_actionFitToHeight_triggered() ------------------------
void Window::on_actionFitToHeight_triggered()
{
    if( !_pGraphicsPixmapItem ) return;

    double pixmapHeight = (double) _pGraphicsPixmapItem->pixmap().height();
    double viewHeight   = (double) graphicsROIEditor->size().height();

    double scale = viewHeight / pixmapHeight;
    
    graphicsROIEditor->setMatrix( QMatrix().scale( scale, scale ), false );
}
// ---------------------------------------------------------------------------









// eof
